package com.lancoo.znbkxx.teacher.ui.mpAndroidChart;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.lancoo.znbkxx.teacher.widget.newBarChart.ValueFormatter;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 饼状图
 * Created by jin
 */

public class PieChartView {

    private PieChart mChart;
    private List<PieEntry> mEntries;
    private String[] labels;
    private String[] labelsX;
    private int[] mPieColors;
    private List<Integer> lineColors;
    private float mTextSize;
    private PieDataSet.ValuePosition mValuePosition;


    public PieChartView(PieChart chart, List<PieEntry> entries,
                        String[] labels,
                        String[] labelsX,
                        int []chartColor,List<Integer> lineColor, float textSize,PieDataSet.ValuePosition valuePosition) {
        this.mChart = chart;
        this.mEntries = entries;
        this.labels= labels;
        this.labelsX= labelsX;
        this.mPieColors = chartColor;
        this.lineColors = lineColor;
        this.mTextSize= textSize;
        this.mValuePosition = valuePosition;
        initPieChart();
    }

    public PieChartView(PieChart chart, List<PieEntry> entries, String[] labels,    String[] labelsX,
                        int []chartColor,List<Integer> lineColor, float textSize) {
        this(chart, entries, labels,labelsX, chartColor,lineColor, textSize, PieDataSet.ValuePosition.INSIDE_SLICE);
    }

    private void initPieChart() {
        mChart.setDragDecelerationFrictionCoef(0.95f);
        mChart.setDrawCenterText(false);
        mChart.getDescription().setEnabled(false);
        mChart.setRotationAngle(-90);//起始角度
        // enable rotation of the chart by touch
        mChart.setRotationEnabled(false);
        mChart.setHighlightPerTapEnabled(true);
        mChart.setDrawEntryLabels(false);//是否显示标注线及Labes内容
        setChartData();
        mChart.animateY(1000, Easing.EaseInOutQuad);

        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(1f);
        l.setYOffset(0f);
        // entry label styling
        mChart.setEntryLabelColor(Color.parseColor("#788aa4"));
        mChart.setEntryLabelTextSize(mTextSize);
        mChart.setExtraOffsets(10, 20, 10, 20);

//        Log.e("MpAndroidChart","========饼图的半径========="+ mChart.getRadius());

    }

    public void setHoleDisenabled () {
        mChart.setDrawHoleEnabled(false);
    }

    /**
     * 中心圆是否可见
     * @param holeColor 中心圆颜色
     * @param holeRadius 半径
     * @param transColor 透明圆颜色
     * @param transRadius 透明圆半径
     */
    public void setHoleEnabled (int holeColor, float holeRadius, int transColor, float transRadius) {
        mChart.setDrawHoleEnabled(true);
        mChart.setHoleColor(holeColor);
        mChart.setTransparentCircleColor(transColor);
        mChart.setTransparentCircleAlpha(110);
        mChart.setHoleRadius(holeRadius);
        mChart.setTransparentCircleRadius(transRadius);
    }

    private void setChartData() {


//        //数据和颜色
//        Integer[] colors=new Integer[]{Color.parseColor("#f9ad4f"),
//                Color.parseColor("#22d7bb"),
//                Color.parseColor("#00afff")};
//        //添加对应的颜色值
//        List<Integer> colorSum = new ArrayList<>();
//        for (Integer color : colors) {
//            colorSum.add(color);
//        }

        PieDataSet dataSet = new PieDataSet(mEntries, "");
        dataSet.setValueTextColors(lineColors);
        dataSet.setSliceSpace(2f);//设置每块饼之间的空隙
        dataSet.setSelectionShift(2f);//点击某个饼时拉长的宽度
        dataSet.setColors(mPieColors);
        dataSet.setYValuePosition(mValuePosition);
        dataSet.setXValuePosition(mValuePosition);
        dataSet.setValueLineColor(0xff000000);  //设置指示线条颜色,必须设置成这样，才能和饼图区域颜色一致
        dataSet.setValueLinePart1OffsetPercentage(115f);
        dataSet.setUsingSliceColorAsValueLineColor(true);
        PieData data = new PieData(dataSet);

        dataSet.setDrawValues(false);//是否显示value

//        dataSet.setValueFormatter(new ValueFormatter() {
//            @Override
//            public String getPieLabel(float value, PieEntry pieEntry) {
////                return pieEntry.getLabel() +"\n"+ value + " %";
//                if (value == 0) {
//                    dataSet.setValueLinePart1Length(0f);//纵标线的长度
//                    dataSet.setValueLinePart2Length(0f);//横标线的长度
//                    return "";
//                } else {
//                    dataSet.setValueLinePart1Length(0.5f);//纵标线的长度
//                    dataSet.setValueLinePart2Length(0.5f);//横标线的长度
////                    BigDecimal b = new BigDecimal(String.valueOf(value));
////                    float num = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
////                    return num + "%";
//
//                    return new DecimalFormat("###").format(value)+"元";
//                }
//            }
//        });

        data.setValueTextSize(mTextSize);
        data.setValueTextColors(lineColors);
        mChart.setData(data);
        // undo all highlights
        mChart.highlightValues(null);
        mChart.invalidate();
        //点击事件处理
        mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {

                if (e==null)return;

                //此时e.getY()等于数据 由此判断点击了哪一个扇区
                if(mEntries.get(0).getValue()==e.getY()){
//                    Log.e("mEntries","=======0000===黄=====");
                    listener.onItemClick(0);
                }else if (mEntries.get(1).getValue()==e.getY()){
//                    Log.e("mEntries","=======1111===绿=====");
                    listener.onItemClick(1);
                }else if (mEntries.get(2).getValue()==e.getY()){
//                    Log.e("mEntries","=======2222===蓝=====");
                    listener.onItemClick(2);
                }
            }

            @Override
            public void onNothingSelected() {
                //图表外部点击事件 也是（上一个方法为点击一次放大） 本方法为二次点击事件
//                Log.e("mEntries","=======图表外部点击事件========");
                listener.onItemClick(5);
            }
        });
    }

    public interface OnItemClickListener {
        public void onItemClick(int position);
    }

    private OnItemClickListener listener; //点击事件的回调

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void refreshPie(){
        initPieChart();
    }

    /**
     * <p>说明文字是否可见</p>
     * @param enabled true 可见,默认可见
     */
    public void setLegendEnabled(boolean enabled) {
        mChart.getLegend().setEnabled(enabled);
        mChart.invalidate();
    }

    public void setPercentValues (boolean showPercent) {
        mChart.setUsePercentValues(showPercent);
    }
}
